<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\home\model;
use think\Model;
use think\Db;
class MenuModel extends Model
{
    /*
    * 操作员信息
    * */
    public function session()
    {
        $user=cmf_get_current_user();
        $cause['id']=$user['id'];
        $users=DB::name('user')->where($cause)->find();
        $where['agency_id']=$users['user_guid'];
        $session=DB::name('shop')->where($where)->find();
        $user['re_guid']=$session['re_guid'];
        $user['shop_name']=$session['shop_name'];
        return $user;
    }
    public function __construct($data = [])
    {
        parent::__construct($data);
        $session=$this->session();
        $this->id=$session['re_guid'];
    }

    /*
     * 类别查询
     * */
    public function index()
    {
        $session=$this->session();
        $where['shop_id']=$session['re_guid'];
        $data = DB::name('goodsclass')->where($where)->order('id desc')->select();
        return $data;
    }
    /*
     * 查询商品
     * 更新商品的查询 以每个店为选择单位
     * 类别查询
     * array_filter 删除数组中空值
     * */
    public function goods($category = '')
    {
        $where['shop_id']=$this->id;
        if($category)
        {
            $where['g_category']=$category;
        }
        $where['status']=1;
        $goods = DB::name('goods')->where($where)->select()->toArray();
        $date=array();
        foreach($goods as $k=>$v)
        {
            $v['stemeal'] = 0;
            $date[$k]=$v;
        }
        return $date;
    }
    /*
     * 套餐查询
     * */
    public function set($array='')
    {
        $where['shop_id']=$this->id;
        if($array)
        {
            $where['set_guid']=$array;
        }
        $data=DB::name('setmeal')->where($where)->order('id desc')->select()->toArray();
        foreach($data as $k=>$v)
        {
            $data[$k]['g_id']=$v['id'];
            $data[$k]['g_price']=$v['set_price'];
            $data[$k]['g_newprice']=$v['set_prices'];
            $data[$k]['goods_name']=$v['set_name'];
            $data[$k]['g_format']="套餐";
            $data[$k]['stemeal']=1;
        }
        return $data;
    }
    /*
         * 展示商品  选择菜单，挂单选择  套餐选择等
         * */
    public function show($where)
    {
        $data=DB::name('order_detail')->where($where)->select()->toArray();
        foreach($data as $k=>$v)
        {
            $formatid=$v['format'];
            $cause['id']=$formatid;
            $format=DB::name('format')->where($cause)->find();
            $data[$k]['formatname']=$format['format'];
        }
        return $data;
    }
    /*
     * 单个商品的查询
     * */
    public function good($id = '')
    {
        $where['g_id'] = $id;
        $where['storeid']=$this->id;
        $array = DB::name('goods')->where($where)->find();
    /*    $time=time();
        $cause['c_goodsid']=$array['g_id'];
        $cause['c_shopid']=$this->id;
        $pro=DB::name('promotion')->where($cause)->find();
        if($pro)
        {
            $start=strtotime($pro['c_start']);
            $end=strtotime($pro['c_end']);
            if($time>$start && $time<$end)
            {
                $array['price']=$pro['c_money'];
                $array['c_id']=$pro['c_id'];
                $array['plan']=$pro['c_plan'];
            }else{
                $array['price']=$array['g_price'];
                $array['c_id']=0;
                $array['plan']='无';
            }
        }else{
            $array['price']=$array['g_price'];
            $array['c_id']=0;
            $array['plan']='无';
        }*/
        return $array;
    }
    /*
     * 求总额
     * */
    public function setpay($session)
    {
        $data = array();
        $data['all_money'] = 0;
        $data['num'] = count($session);
        foreach ($session as $k => $v) {
            $data['all_money'] = $data['all_money'] + $v['price'];
        }
        return $data;
    }

    /*
    * 求折扣总和 和需要支付的总额
    * c_rmbs 折扣优惠总额
    * all_money  需要付款总额
    * s_pay 实际付款
    *套餐不牵扯到打折
    * */
    public function setpays($session)
    {
        $data = array();
        $data['c_rmbs'] = 0;
        $data['all_money'] = 0;
        foreach ($session as $k => $v) {

            $data['all_money'] = $data['all_money'] + $v['g_price'];
            $data['c_rmbs'] = $data['c_rmbs'] + $v['c_rmb'];

        }
        $data['s_pay'] = $data['all_money'] - $data['c_rmbs'];
        return $data;
    }

    /*
     * 查询折扣
     * */
    public function promotion($where = "")
    {
        $data = DB::name('promotion')->where($where)->select();
        return $data;
    }

    /*
     * 从数据库中查询
     * */
    public function selorder($order = '')
    {
        $where['orderid'] = $order;
        $data = DB::name('order_detail')->where($where)->order('ctime desc')->select()->toArray();
        return $data;
    }
    /*
     *查询总额
     * */
    public function selorders($order = '')
    {
        $where['orderNO'] = $order;
        $data = DB::name('order')->where($where)->field('orderNo , actualMoney')->find();
        return $data;
    }
    /*
     *  2018-05-31  会员价格有会员价的按照会员价格计算  没有会员价的原价计算  折扣商品不参与会员价格
     * 分别查询出折扣和没有折扣的商品
     * 套餐  有更好的方法时候在进行更换
     * */
    public function  vip($order=" ")
    {
        $where['orderid']=$order;
        //分别查询出折扣和没有折扣的商品
        $data=DB::name('order_detail')->where($where)->select()->toArray();
        $money=0;
        foreach($data as $k=>$v)
        {
            if($v['stemeal']==0)
            {
                if($v['rebateid']==0)
                {
                    if($v['vipprice']==0)
                    {
                        $money +=$v['money'];
                    }else{
                        $money +=$v['vipprice'];
                    }
                }else{
                    $money +=$v['money'];
                }
            }elseif($v['stemeal']==1){
                $money +=$v['money'];
            }
        }
         return $money;
    }
    /*
        * 订单存储
        *  order  订单表
        *  order_detail  订单详情表
        *  2018/05/16 完善折扣存储
        * 2018/7-31 王磊  同一个订单的同一款产品同一种商品状态订单详情表中存储到一起 在数据库中添加相同小计和数量
        * 同一个订单 同一款产品的不同状态下 分开存储
        * v:表示数据
        * session 操作员
        * array 接收值
        * */
   public function saveorder($v, $array)
    {
        $order = $this->saves($v, $session, $array);
        $where['goodsid']=$array['id'];
        $where['orderid']=$order['orderNo'];
        $where['nature']=$array['nature'];
        $old_detail=DB::name('order_detail')->where($where)->find();
        if($old_detail)
        {
            if($v['c_id']==0)
            {
                $data['total']=$old_detail['total']+$v['g_price'];
            }else{
                $data['total']=$old_detail['total']+$v['price'];
            }
            $data['count']=$old_detail['count']+1;
            DB::name('order_detail')->where($where)->update($data);
            $order['id']=$old_detail['id'];
        }else{
            $runt = rand(1000,9999);
            $id=$session['store_id'].time().$runt;//2018/05/30 name 乾坤;
            $data['id'] = $id;
            $data['orderid'] = $order['orderNo'];
            if($v['setmeal']==0)
            {
                $data['format'] = $v['g_format'];
                $data['price'] = $v['g_price'];
                $data['vipprice'] =$v['g_vipprice'] ;
                $data['g_goodsname'] = $v['g_goodsname'];
                $data['goodsid'] = $v['g_id'];
                $data['stemeal'] = 0;
                if($v['c_id']==0)
                {
                    $data['money'] = $v['g_price'];
                    $data['total'] =$v['g_price'];
                }else{
                    $data['money'] = $v['price'];
                    $data['total'] = $v['price'];
                }
            }else{
                $data['format'] = 0;
                $data['money'] =$v['set_price'];
                $data['price'] = $v['set_price'];
                $data['g_goodsname'] = $v['set_name'];
                $data['goodsid'] = $v['set_id'];
                $data['stemeal'] = 1;
                $data['vipprice'] = 0;
            }
            $data['integral'] = 0;
            $data['rmb'] = 0;
            $data['rebateid'] = 0;
            $data['storeid'] =$session['store_id'];
            $data['ctime'] = $order['ctime'];
            $data['paystatus'] = 0;
            $data['nature']=0;
            DB::name('order_detail')->insert($data);
            $order['id']=$id;
        }
        return $order;
    }

    /*
     * 订单order表
     * 2018-06-07 添加一个当天销售数量
     * 2018-08-03 先判断订单详情表中是否存在相同的商品
     * **/
   public function saves($order, $session, $array)
    {
        if (!empty($array['orderid'])) {
            $where['order_no'] = $array['orderid'];
            $old = DB::name('order')->where($where)->find();
            if($order['setmeal']==0)
            {
                $new_money = $old['total_money'] + $order['g_price'];
                $actualMoney = $old['actual_money'] + $order['price'];
            }else{
                $new_money = $old['total_money'] + $order['set_price'];
                $actualMoney = $old['actual_money'] + $order['set_price'];
            }
            $date['total_money'] = $new_money;
            $date['actual_money'] = $actualMoney;
            DB::name('order')->where($where)->update($date);
            $data['order_no'] = $array['orderid'];
            $data['c_time'] = $old['ctime'];
        } else {
            $today_time=strtotime('today');
            $night=$today_time+86400;
            $time=time();
            if($time<$today_time ||$time>$night)
            {
                $num=1;
            }else{
                $where['storeid']=$this->id;
                $where['ctime']=array('between',array($today_time,$night));
                $lastorder=DB::name('order')->where($where)->order('ctime desc')->find();
               
                if($lastorder['num']>0 && $lastorder['num']<100){
                    $num=$lastorder['num']+1;
                }else{
                    $num=1;
                }
            }
            $times = date('YmdHis', time());
            $data = array();
            $data['id'] = $session['store_id'].time().rand(1000,9999);//2018/05/30 name 乾坤
            $data['orderNo'] = duid();
            $data['operatorid'] =$session['id'] ;
            $data['storeid'] = $this->id;
            if($order['setmeal']==0)
            {
                $data['totalMoney'] = $order['price'];
                $data['actualMoney'] = $order['price'];
            }else{
                $data['actualMoney'] = $order['set_price'];
                $data['totalMoney'] = $order['set_price'];
            }
            $data['num']=$num;
            $data['discount'] = 0;
            $data['getMoney'] = 0;
            $data['giveChange'] = 0;
            $data['payType'] = 0;
            $data['payStatus'] = 0;
            $data['takeOut'] = 0;
            $data['userName'] = 0;
            $data['userMobile'] = 0;
            $data['userAddress'] = 0;
            $data['ctime'] = $time;
            $data['utime'] = 0;
            $state = DB::name('order')->insert($data);

        }
        $count['orderNo']=$data['orderNo'];
        $count['ctime']=$data['ctime'];
        return $count;
    }
    /*
     * 挂单状态
     * */
    public function updata($data='',$where='')
    {
        $state=DB::name('order')->where($where)->update($data);
        return $state;
    }
    /*
     * 商品删除操作
     * 2018-05-19 删除不在用数组 k值用商品id 王磊
     * 点击减号是单个结算  如果是删除话全部结算
     * 如果订单详情表中没有商品了就  就删除订单表的记录
     * */
    public function del($orderid,$id)
    {
        $where['goodsid']=$id;
        $where['orderid']=$orderid;
        $state=DB::name('order_detail')->where($where)->find();
        $price=$state['money'];
        if($state)
        {
            $cause['orderNo']=$orderid;
            $old=DB::name('order')->where($cause)->find();
            $new=$old['totalMoney']-$price;
            $array['totalMoney']=$new;
            $new=$old['actualMoney']-$price;
            $array['actualMoney']=$new;
            $s=DB::name('order')->where($cause)->update($array);
            $where['id']=$state['id'];
            $ss=DB::name('order_detail')->where($where)->delete();
        }
        $data=DB::name('order_detail')->where(['orderid'=>$orderid])->select()->toArray();
        if($data)
        {
            /*
             * 如果查询出同种类型商品的是否还有有
             * */
            $why['orderid']=$orderid;
            $why['goodsid']=$id;
            $why['nature']=array('eq','无');
            $state=DB::name('order_detail')->where($why)->find();
            if($state)
            {
                $arrays['id']=$state['id'];
            }else{
                $arrays['id']=0;
            }
                $arrays['orderNo']=$orderid;
        }else{
            DB::name('order')->where(['orderNo'=>$orderid])->delete();
            $arrays=0;
        }
        return  $arrays;
    }
     /*查询属性*/
     public function goods_state($array=null)
     {
         $where=$array;
         $where['status']=1;
         $status=DB::name('goods_state')->where($where)->select()->toArray();
         return $status;

     }
     /*订单列表*/
    public  function content($orderid)
    {

        $orders=$this->selorder($orderid);
        foreach($orders as $k=>$v)
        {
            $fid=$v['stemeal'];
            if($fid == 1)
            {
                $format_name="套餐";
                $orders[$k]['format']=$format_name;
            }
            if($v['nature']==0)
            {
                $orders[$k]['naturename']='无';
            }else{
                $where['id']=$v['nature'];
                $nature_array=DB::name('goods_state')->where($where)->find();
                $orders[$k]['naturename']=$nature_array['state_name'];
            }
        }
        $vip=$this->vip($orderid);
        $all=$this->selorders($orderid);
        $pay['num']=count(DB::name('order_detail')->where(['orderid'=>$orderid])->select());
        $pay['orderid']=$all['orderNo'];
        $pay['all_money']=$all['actualMoney'];
        $pay['vip']=$vip;
        $date['data']=$orders;
        $date['pay']=$pay;
        return $date;
    }

}